﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaseManagerLibrary.WinForm
{
    public partial class Paging : UserControl
    {
        public delegate void PageChangedEventHandler(int pageNo, int pageSize);
        public event PageChangedEventHandler OnPageChanged;

        private int total = 1;
        public int Total
        {
            get { return total; }
            set
            {
                total = value;
                lblTotal.Text = total.ToString() + "  页";
            }
        }

        private int pageNo = 1;
        public int PageNo
        {
            get { return pageNo; }
            set
            {
                txtPageNo.Text = value.ToString();

                //if (pageNo != value)
                //{
                OnPageChanged?.Invoke(value, PageSize);
                //}
                pageNo = value;
            }
        }

        public int PageSize { get; set; }

        public Paging()
        {
            InitializeComponent();

            Total = 1;
            PageNo = 1;
            PageSize = 50;
        }

        public void Reset()
        {
            pageNo = 1;
            txtPageNo.Text = "1";
            total = 1;
            lblTotal.Text = "1  页";
        }
        private void lblFirst_Click(object sender, EventArgs e)
        {
            PageNo = 1;
        }

        private void lblPrev_Click(object sender, EventArgs e)
        {
            if (pageNo > 1)
            {
                PageNo -= 1;
            }
        }

        private void lblNext_Click(object sender, EventArgs e)
        {
            if (pageNo < total)
            {
                PageNo += 1;
            }
        }

        private void lblLast_Click(object sender, EventArgs e)
        {
            PageNo = total;
        }

        private void txtPageNo_TextChanged(object sender, EventArgs e)
        {
            PageNo = Convert.ToInt32(txtPageNo.Text);

            if (PageNo < 1)
            {
                PageNo = 1;
            }
        }

        private void txtPageNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键  
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数  
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符  
                }
            }
        }
    }
}
